﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace VietNamKey
{
    public partial class ucInfo : UserControl
    {
        public ucInfo()
        {
            InitializeComponent();
            lblHeader.Text = "Việt Nam Keyboard " + Global.Version;
        }

        private void btnCheckForUpdate_Click(object sender, EventArgs e)
        {
        	MethodInvoker  Showbtn = delegate(){btnDownload.Visible = true;};
        	MethodInvoker  Hidebtn = delegate(){btnDownload.Visible = false;};
        	Thread Loading = new Thread(new ThreadStart(delegate(){
            	lblUpdateInfo.Text = "";
            	while(true)
            	{
            		lblUpdateInfo.Text += " * ";
            		if(lblUpdateInfo.Text.Length > 30)
            			lblUpdateInfo.Text = "";
            		Thread.Sleep(500);
            	}
            }));
        	Thread Update = new Thread(new ThreadStart(delegate(){
               	btnDownload.Enabled = false;
	            if (new Ping().Send("www.google.com.vn").Status == IPStatus.Success)
	            {
	                try
	                {
	                    var NewVer = OpenWebFile("http://hoanchan.droppages.com/VietNamKey.txt");
	                    if (NewVer == Global.Version)
	                    {
	                    	Loading.Abort();
	                        lblUpdateInfo.Text = "Bạn đang sử dụng phiên bản mới nhất";
	                        btnDownload.BeginInvoke(Hidebtn);
	                    }
	                    else
	                    {
	                    	Loading.Abort();
	                        lblUpdateInfo.Text = "Hiện có phiên bản mới là " + NewVer;
	                        btnDownload.BeginInvoke(Showbtn);
	                    }
	                }
	                catch
	                {
	                	Loading.Abort();
	                    lblUpdateInfo.Text = "Không thể kết nối tới máy chủ!";
                        btnDownload.BeginInvoke(Hidebtn);
	                }
	            }
	            else
	            {
	            	Loading.Abort();
	                lblUpdateInfo.Text = "Bạn đang không kết nối Internet!";
                    btnDownload.BeginInvoke(Hidebtn);
	            }
	            btnDownload.Enabled = true;
            }));
        	Loading.Start();
        	Update.Start();
        }

        private string OpenWebFile(string Link)
        {
            WebClient client = new WebClient();
            Stream stream = client.OpenRead(Link);
            StreamReader reader = new StreamReader(stream);
            return reader.ReadToEnd();
        }

        private void btnDownload_Click(object sender, EventArgs e)
        {
            Process.Start("http://hoanchan.droppages.com/VietNamKey.zip");
        }
    }
}
